package cn.ren.controller;

import cn.ren.constant.MessageConstant;
import cn.ren.constant.RedisConstant;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.entity.Result;
import cn.ren.pojo.Address;
import cn.ren.pojo.CheckItem;
import cn.ren.pojo.Setmeal;
import cn.ren.service.AddressService;
import cn.ren.util.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/address")
public class DJ_AddressController {

    @Reference
    private AddressService addressService;

    @Autowired //注入jedis连接池
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
            //获取文件原始名
            String originalFilename = imgFile.getOriginalFilename();
            //文件后缀
            int index = originalFilename.lastIndexOf(".");
            String substring = originalFilename.substring(index);
            //生成唯一标识文件名
            UUID uuid = UUID.randomUUID();
            String fileName = uuid.toString() + substring;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            /*保存图片名称到redis里保存所有图片set集合中*/
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            //图片上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result dj_add(@RequestBody Address address){
        System.out.println(address);
        try {
            addressService.add(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增体检机构地址失败");
        }
        return new Result(true,"新增体检机构地址成功");
    }

    @RequestMapping("/findPage")
    public PageResult dj_findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        return addressService.queryPage(currentPage,pageSize,queryString);
    }

    @RequestMapping("/edit")
    public Result dj_edit(@RequestBody Address Address){
        System.out.println(Address);
        try {
            addressService.edit(Address);
        }catch (Exception e){
            return new Result(false,"预约地址修改失败");
        }
        return new Result(true,"预约地址修改成功");
    }

    @RequestMapping("/del")
    public Result dj_del(Integer id){
        System.out.println(id);
        try {
            addressService.del(id);
        }catch (Exception e){
            return new Result(false,"预约地址删除失败");
        }
        return new Result(true,"预约地址删除成功");
    }

}
